﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormMesPgd
    Public usr As String
    Dim objPgdMes As New clsPgdMes
    Dim httpHelper As New HttpHelper
    Dim txt As TextBox
    Private Sub FormMesPgd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startDate As String = Date.Now.ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        setDgvData()
    End Sub

    Private Sub btnMesPlan_Click(sender As Object, e As EventArgs) Handles btnMesPlan.Click
        Dim params As String = "{""workshop"":""装配"",""startdate"":""" & DateTime.Parse(txtStartDate.Text).ToString("yyyy-MM-dd") & """,""enddate"":""" & DateTime.Parse(txtEndDate.Text).ToString("yyyy-MM-dd") & """,""compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""}"
        Dim rtn As String = httpHelper.HttpPostWebService("findDispatchOrderInfoListByTimeAndCJ", params)
        'MsgBox(rtn)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        'MsgBox(code)
        Dim data As String = jo("data").ToString() '输出0
        'data = "[{""dispatchingorder"":""PGDH211103001"",
        '""start_time"":""2021-11-02 00:00:00"",
        '""end_time"":""2021-11-02 23:59:59"",
        '""project_code"":""fgm-test-0001"",
        '""project_name"":""fgm-test-0001"",
        '""equip_id"":""070046,070023,070020,070011,"",
        '""jp_type"":""mes不存在"",
        '""prod_spec_id"":""HDR-0.4-201-035-18"",""count"":9.0},
        '{""dispatchingorder"":""PGDH211103222"",""start_time"":""2021-11-02 00:00:00"",
        '""end_time"":""2021-11-02 23:59:59"",
        '""project_code"":""fgm-test-0001"",
        '""project_name"":""fgm-test-0001"",
        '""equip_id"":""070046,070023,070020,070011,"",
        '""jp_type"":""mes不存在"",
        '""prod_spec_id"":""HDR-0.4-201-035-18"",""count"":9.0}]"
        'MsgBox(data)
        Dim jarray As JArray = JArray.Parse(data)
        If jarray.Count > 0 Then
            For index = 0 To jarray.Count - 1
                Dim en As New entityPgdMes
                en.pgd_code = jarray(index)("dispatchingorder")
                en.oper_date = DateTime.Parse(jarray(index)("start_time")).ToShortDateString()
                en.start_time = jarray(index)("start_time").ToString()
                en.end_time = DateTime.Parse(jarray(index)("start_time")).AddDays(1).ToString()
                en.support_model = jarray(index)("p_prod_spec_id")
                en.type_value = jarray(index)("prod_spec_id")
                en.mes_project_code = jarray(index)("project_code")
                en.mes_project_name = jarray(index)("project_name")
                en.oper_count = jarray(index)("count")
                en.workshop_id = 5
                en.validity = "1"
                en.status = "1"
                en.origin = "0"
                en.Ope = usr
                en.jp_type = String.Empty
                en.note = String.Empty
                Dim dtmesdata As DataTable = objPgdMes.getListByPgd(jarray(index)("dispatchingorder").ToString())
                If dtmesdata.Rows.Count = 0 Then
                    Dim rtn_save As Integer = objPgdMes.insert(en)
                Else
                    Dim rtn_save As Integer = objPgdMes.update(en)
                End If
            Next
        Else
            MsgBox("没有数据", MsgBoxStyle.Information, "喷漆")
        End If
    End Sub

    Private Sub setDgvData()
        Dim dt As DataTable = objPgdMes.getPgd(5, txtStartDate.Text, txtEndDate.Text)
        If dt.Rows.Count > 0 Then
            dt.Columns.Add("validity_name")
            For Each row As DataRow In dt.Rows
                If row("validity") = "0" Then
                    row("validity_name") = "无效"
                Else
                    row("validity_name") = "有效"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim b_PgdMesProjectCode As String = "GⅡ型700支座-结构七-S-CL-003"
        Dim b_PgdMes_gx As String = "01.02.038"
        Dim warehousecode As String = "ZAGZ06"
        Dim rnt_mes_bg As String = mesbgOk("1", b_PgdMes_gx, b_PgdMesProjectCode)
        Dim xdfd As String = rnt_mes_bg
        'mesbgNg("1", b_PgdMes_gx, b_PgdMesProjectCode, "")
        Dim rnt_mes_rk As String = mesRk("1", warehousecode,
                                                        b_PgdMes_gx,
                                                        b_PgdMesProjectCode)
        Dim jo_rk As JObject = JsonConvert.DeserializeObject(rnt_mes_rk)
        Dim code_rk As String = jo_rk("code").ToString() '输出0
        Dim msg_rk As String = jo_rk("msg").ToString() '输出操作成功
    End Sub

    Private Function mesbgOk(oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "装配" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Return httpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "装配" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103105""" '部门id
        params &= "}"
        Return httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
    End Function

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

End Class